<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%score}}".
 *
 * @property integer $id
 * @property integer $win
 * @property integer $role_id
 * @property integer $score
 * @property string $create_at
 */
class Score extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%score}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['win', 'role_id', 'score'], 'required'],
            [['win', 'role_id', 'score'], 'integer'],
            [['create_at'], 'safe'],
            [['win', 'role_id'], 'unique', 'targetAttribute' => ['win', 'role_id'], 'message' => '这条规则已经存在']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'win' => Yii::t('app', '胜负'),
            'role_id' => Yii::t('app', '角色'),
            'score' => Yii::t('app', '得分'),
            'create_at' => Yii::t('app', '创建时间'),
        ];
    }

    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->create_at = date('Y-m-d H:i:s');
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    public function getRole(){
        return $this->hasOne(Role::className(), ['id' => 'role_id']);
    }

    public function getWins(){
        return $this->hasOne(Win::className(), ['id' => 'win']);
    }
}
